package com.apes.scm.statement.customerConsignStatement.model;

import com.apes.framework.jpa.BaseModel;
import com.apes.framework.jpa.DefaultPersistListener;
import com.apes.framework.jpa.comment.Comment;
import com.apes.framework.jpa.generator.ApesGenerator;
import com.apes.framework.util.ApesConst;
import com.apes.framework.util.Arith;
import com.apes.scm.account.account.model.InventoryConsign;
import com.apes.scm.account.tax.model.Tax;
import com.apes.scm.masterdata.product.model.Product;
import com.apes.scm.masterdata.product.model.ProductUom;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.SelectBeforeUpdate;

import javax.persistence.*;
import javax.validation.constraints.NotNull;

@Getter
@Setter
@SelectBeforeUpdate
@DynamicInsert
@DynamicUpdate
@Entity
@EntityListeners(DefaultPersistListener.class)
@Table(name =CustomerConsignStatementItem.CONST_TABLE_NAME,
        indexes = {
                @Index(name = "idx_statement_id", columnList = "statement_id")
        })
public class CustomerConsignStatementItem extends BaseModel {
    public static final String CONST_TABLE_NAME = "client_consign_statement_item";

    @Id
    @Column(name = "id", length = 32)
    @ApesGenerator(code = CONST_TABLE_NAME, name = "客户寄售对账单明细")
    @Comment("对账明细单号")
    private String id;

    @ManyToOne(optional = false,fetch = FetchType.LAZY)
    @JoinColumn(name="statement_id", referencedColumnName="id", nullable = false)
    @Comment("对账单单号")
    private CustomerConsignStatement customerConsignStatement;


    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "inventory_consign_id", referencedColumnName = "id")
    @Comment("客户寄售库存")
    private InventoryConsign inventoryConsign; //客户寄售库存


    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name="product_id", referencedColumnName="id", nullable = false)
    @Comment("商品")
    private Product product;

    @NotNull
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "product_uom_id", referencedColumnName = "id")
    @Comment("计量单位编码")
    private ProductUom uom;

    @NotNull
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "tax_id", referencedColumnName = "id")
    @Comment("税率编码")
    private Tax tax;


    @Column(name = "price_unit", nullable = false, columnDefinition = ApesConst.FIELD_16_4)
    @Comment("单价")
    private double priceUnit = 0;

    @Column(name = "pending_qty", nullable = false, columnDefinition = ApesConst.FIELD_16_4_0)
    @Comment("待对账数量")
    private double pendingQty = 0;

    @Column(name = "statement_qty", nullable = false, columnDefinition = ApesConst.FIELD_16_4)
    @Comment("对账数量")
    private double statementQty = 0;


    @Column(name = "amount", nullable = false, columnDefinition = ApesConst.FIELD_16_4)
    @Comment("对账金额")
    private double amount = 0;



    public void computeAmt() {
        this.amount = Arith.round(this.priceUnit * this.statementQty,2);
    }


}
